package com.kaaction.modules.mer.api;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.utils.StringUtils;
import com.kaaction.common.web.BaseController;
import com.kaaction.modules.mer.entity.WeddingGiftDetail;
import com.kaaction.modules.mer.service.WeddingGiftDetailService;

@Controller
@RequestMapping(value = "/api/v1/weddingGiftApi")
public class WeddingGiftApiController extends BaseController{
	
	@Autowired
	WeddingGiftDetailService weddingGiftDetailService;
	
	/**
	 * 礼金记账新增与更新
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/addWeddingGift")
	public void addWeddingGift(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException, ParseException {
		
		
		WeddingGiftDetail  weddingGiftDetail = new WeddingGiftDetail();
		
		String thirdpartyId = request.getParameter("thirdpartyId");//第三方id
		
		String id = request.getParameter("id");//宾客id
		String name = request.getParameter("name");//宾客名
		Double price = Double.valueOf(request.getParameter("price"));//礼金
		String remarks = request.getParameter("remarks");//备注
		
		String addDate = request.getParameter("addDate");//收礼金时间
		Date da = null;
		if(!StringUtils.isBlank(addDate)){
			 da = new SimpleDateFormat("yyyy-MM-dd").parse(addDate);
		}else{
			 da = new Date();
		}
		
		weddingGiftDetail.setName(name);
		weddingGiftDetail.setPrice(price);
		weddingGiftDetail.setThirdpartyId(thirdpartyId);
		weddingGiftDetail.setRemarks(remarks);
		weddingGiftDetail.setAddDate(da);
		weddingGiftDetail.setId(id);
		
		weddingGiftDetailService.save(weddingGiftDetail);
		super.writeAjaxResponse(response,"");
	}
	
	/**
	 * 删除礼金记账
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/deleteWeddingGift")
	public void deleteWeddingGift(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException, ParseException {
		
		WeddingGiftDetail  weddingGiftDetail = new WeddingGiftDetail();
		String id = request.getParameter("id");
		weddingGiftDetail.setId(id);
		weddingGiftDetailService.delete(weddingGiftDetail);
		super.writeAjaxResponse(response,"");
	}
	
	/**
	 * 礼金记账查询
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selWeddingGift")
	public void selWeddingGift(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		WeddingGiftDetail  weddingGiftDetail = new WeddingGiftDetail();
		
		String thirdpartyId = request.getParameter("thirdpartyId");//第三方id
		
		weddingGiftDetail.setThirdpartyId(thirdpartyId);
		
		List<Map<String,Object>> listCashGift = weddingGiftDetailService.selWeddingGiftDetail(weddingGiftDetail);
		
		int moneyTotal = weddingGiftDetailService.selMoneyTotal(weddingGiftDetail);
		
		map.put("listCashGift", listCashGift);
		map.put("moneyTotal", moneyTotal);
		
		super.writeAjaxResponse(response,map);
	}
	
	
	/**
	 * 礼金记账更新初始化
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/getWeddingGift")
	public void getWeddingGift(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		
		WeddingGiftDetail  weddingGiftDetail = new WeddingGiftDetail();
		
		String id = request.getParameter("id");//宾客id
		
		weddingGiftDetail.setId(id);
		
		super.writeAjaxResponse(response,weddingGiftDetailService.get(weddingGiftDetail));
	}

}
